<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
class Setting_model extends CI_Model{
    public function __construct(){
        parent::__construct();
        //Load database driver
        $this->load->database();
        
        //Session
        $this->load->library('session');
    }
    //Hàm lấy list module
    public function listModule(){
        $this->db->order_by('mod_order','ASC');
        $query = $this->db->get('modules');
        return $query->result_array();
    }
    
    //Hàm thêm module
    public function addModule($mod_name,$mod_control,$mod_view,$mod_action_name,$mod_action_link,$mod_order){
        $array_data = array(
                            'mod_name'=>$mod_name,
                            'mod_control'=>$mod_control,
                            'mod_view'=>$mod_view,
                            'mod_action_name'=>$mod_action_name,
                            'mod_action_link'=>$mod_action_link,
                            'mod_order'=>$mod_order      
                      );
        //Câu lệnh update
        $result = $this->db->insert('modules',$array_data);
        //Kết quả trả về
        if(!$result){
            return 'Đã có lỗi xảy ra';
        }
    }
    public function editModule($mod_id,$mod_name,$mod_control,$mod_view,$mod_action_name,$mod_action_link,$mod_order){
        $array_data = array(
                            'mod_name'=>$mod_name,
                            'mod_control'=>$mod_control,
                            'mod_view'=>$mod_view,
                            'mod_action_name'=>$mod_action_name,
                            'mod_action_link'=>$mod_action_link,
                            'mod_order'=>$mod_order      
                      );
        //Câu lệnh insert
        $result = $this->db->update('modules',$array_data,'mod_id = '.$mod_id);
        return $result;
    }
    public function deleteModule($mod_id){
        $result = $this->db->delete('modules','mod_id = '.$mod_id);
        if(!$result){
            return 'Không thể xóa bản ghi này';
        }
        else{
            return 'Success';
        }
    }
    
    /**
     *      Quản lý users
     * */
    //Hàm lấy list users
    public function listUsers(){
        $this->db->order_by('adu_id','ASC');
        $query = $this->db->get('admin_users');
        return $query->result_array();
    } 
    public function addUser($adu_name,$adu_pass,$adu_fullname,
                            $adu_phone,$adu_address,$adu_mail,
                            $adu_yahoo,$adu_skype,$adu_birthday,
                            $adu_active,$adu_role){
        $arr_data = array('adu_name'=>$adu_name,
                          'adu_pass'=>md5($adu_pass),
                          'adu_fullname'=>$adu_fullname,
                          'adu_phone'=>$adu_phone,
                          'adu_address'=>$adu_address,
                          'adu_mail'=>$adu_mail,
                          'adu_yahoo'=>$adu_yahoo,
                          'adu_skype'=>$adu_skype,
                          'adu_birthday'=>$adu_birthday,
                          'adu_active'=>$adu_active,
                          'adu_role'=>$adu_role);
        $result = $this->db->insert('admin_users',$arr_data);
        return $result;
    }    
    public function editUser($adu_id,$data){
        return $this->db->update('admin_users',$data,'adu_id = '.$adu_id);
    }
    public function deleteUser($id){
        return $this->db->delete('admin_users',array('adu_id'=>$id));
    }
    public function getPermission($adu_id,$mod_id){
        //Lấy quyền của user về module này
        $this->db->select('adu_edit,adu_delete,adu_add');
        $query = $this->db->get_where('admin_users_right',array('adu_admin_id'=>$adu_id,
                                                                'adu_mod_id'=>$mod_id));
                                                                
        //Mảng quyền sử dụng
        if($query->num_rows()){
            $permission = array(
                    'add'=>$query->row()->adu_add,
                    'edit'=>$query->row()->adu_edit,
                    'delete'=>$query->row()->adu_delete
                    );  
        }
        else{
            $permission = array(
                    'add'=>0,
                    'edit'=>0,
                    'delete'=>0
                    );
        };
        return $permission;
    }
    public function removePermission($adu_id){
        return $this->db->delete('admin_users_right',array('adu_admin_id'=>$adu_id));
    }
    public function setPermission($adu_id,$mod_id,$add,$edit,$delete){
        return $this->db->insert('admin_users_right',array('adu_admin_id'=>$adu_id,
                                                            'adu_mod_id'=>$mod_id,
                                                            'adu_add'=>$add,
                                                            'adu_edit'=>$edit,
                                                            'adu_delete'=>$delete
                                                            ));
    }
}
?>